package com.zhuss.repair.processor;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.bo.repair.repairAudit.ChaKanBo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.repair.db.dao.ext.RepairDao;
import com.zhuss.repair.db.entity.ChaKan;
import com.zhuss.repair.db.entity.ChaKan_DengJi;
import com.zhuss.repair.db.entity.ChaKan_HeCha;
import com.zhuss.repair.db.entity.ChaKan_KanCha;
import com.zhuss.repair.db.entity.ChaKan_PingJia;
import com.zhuss.repair.db.entity.ChaKan_WeiXiu;
import com.zhuss.repair.db.entity.Goods;
import com.zhuss.util.JsonUtil;

/**
 * 删除餐品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("repairChaKan")
public class RepairChaKan extends AbstractProcessor<Object, ChaKanBo> implements Processor<Object, ChaKanBo> {


    @Autowired
    private RepairDao repairDao;
    
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
    /**
     * 校验
     */
	@Override
	@SuppressWarnings("unchecked")
    public void check(ChaKanBo bo) throws WpsException {
    	/*Long repairId = bo.getRepairId();
    	List<Long> permissionIdList = (List<Long>) repairPermissionRead.listPermissionId(repairId);
    	if (!permissionIdList.isEmpty()) {
    		throw new WpsException(ExceptionCode.不能删除已绑定权限的餐品);
    	}
    	List<Long> userIdList = (List<Long>) userRepairRead.listUserId(repairId);
    	if (!userIdList.isEmpty()) {
    		throw new WpsException(ExceptionCode.不能删除已绑定用户的餐品);
    	}*/
    }
    
    /**
     * 操作方法(反馈)
     */
    @Override
    public Object process(ChaKanBo param) throws WpsException {
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("step",param.getStep());
    	map.put("repair_id",param.getRepair_id());
    	ChaKan result = new ChaKan();
    	Integer step = 1;
    	if(param.getStep()!=null){
    		step = param.getStep();
    	}
    	
    	//1.登记
    	if(step>=1){
    		ChaKan_DengJi dengji = repairDao.getChaKan_DengJi(map);
    		
    		DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKey(dengji.getRepair_files());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			List<String> urlList = downloadVo.getUrlList();
			dengji.setPic(urlList);
    		
    		result.setDengji(dengji);
    	}
    	//2.派单
    	if(step>=2){
    		result.setPaidan(repairDao.getChaKan_PaiDan(map));
    	}
		//3.勘察
    	if(step>=3){
	    	ChaKan_KanCha kancha = repairDao.getChaKan_KanCha(map);
	    	List<Goods> kanchaGoodsList = repairDao.getKanCha_Goods(map);
	    	kancha.setGoodsList(kanchaGoodsList);
	    	result.setKancha(kancha);
    	}
		//4.维修
    	if(step>=4){
	    	ChaKan_WeiXiu weixiu = repairDao.getChaKan_WeiXiu(map);
	    	List<Goods> weixiuGoodsList = repairDao.getKanCha_Goods(map);
	    	weixiu.setGoodsList(weixiuGoodsList);
	    	result.setWeixiu(weixiu);
    	}
    	//5.核查
    	if(step>=5){
	    	ChaKan_HeCha hecha = repairDao.getChaKan_HeCha(map);
	    	result.setHecha(hecha);
    	}
    	//6.评价
    	if(step>=6){
	    	ChaKan_PingJia pingjia = repairDao.getChaKan_PingJia(map);
	    	result.setPingjia(pingjia);
    	}
        return result;
      
    }
    
    

    
    /**
     * 操作日志
     */
	@Override
	public Object log(ChaKanBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.报修勘察.key());
    	record.setAction(LogActionEnum.报修勘察.key());
    	/*record.setTargetId(param.getRepairId().longValue());*/
    	/*record.setTargetName(param.getRemark());*/
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
